using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Skeletons
{
    public partial class FormDBScheletroLoad : Form
    {
        private DB m_rDB;
        private Form1 m_rParent;
        private string m_strItemXML = "";

        private class FormDBMovimentoCaricaListItem
        {
            private string m_strLabel;
            private long m_lID;

            public FormDBMovimentoCaricaListItem(string p_strLabel, long p_lID)
            {
                m_strLabel = p_strLabel;
                m_lID = p_lID;
            }

            public long ID
            {
                get { return m_lID; }
            }

            public override string ToString()
            {
                return m_strLabel;
            }
        }

        public FormDBScheletroLoad(Form1 p_rParent)
        {
            InitializeComponent();

            m_rParent = p_rParent;
            m_rDB = m_rParent.getDB();

            txtDescrizione.Text = "<seleziona>";
            txtDataAggiornamento.Text = "<seleziona>";
            txtDataCreazione.Text = "<seleziona>";
            txtCategoria.Text = "<seleziona>";
            txtPersonaggio.Text = "<seleziona>";
        }

        private void load(long p_lID)
        {
            DBTableScheletro itm = new DBTableScheletro(m_rDB, p_lID);

            txtDescrizione.Text = itm.Descrizione;
            txtDataCreazione.Text = itm.DataCreazione;
            txtDataAggiornamento.Text = itm.DataAggiornamento;
            txtCategoria.Text = itm.IDCategoria.ToString();
            txtPersonaggio.Text = itm.IDPersonaggio.ToString();

            m_strItemXML = itm.XML;
        }

        private void loadNames()
        { 
            if (m_rDB.isConnected())
            {
                MySql.Data.MySqlClient.MySqlDataReader dbReader = m_rDB.query("SELECT M_ID, M_Nome FROM Movimento");
                if (dbReader != null)
                {
                    if (dbReader.HasRows)
                    {
                        while (dbReader.Read())
                        {
                            lstScheletri.Items.Add(new FormDBMovimentoCaricaListItem(dbReader.GetString("M_Nome"), dbReader.GetInt64("M_ID")));
                        }
                    }
                    dbReader.Close();
                }
            }
        }

        private void FormDBMovimentoCarica_Load(object sender, EventArgs e)
        {
            loadNames();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void lstMovimenti_SelectedIndexChanged(object sender, EventArgs e)
        {
            long id = ((FormDBMovimentoCaricaListItem)lstScheletri.Items[lstScheletri.SelectedIndex]).ID;
            load(id);
        }

        private void btnCArica_Click(object sender, EventArgs e)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(m_strItemXML);
            m_rParent.setAnimatorFromXML(doc.SelectSingleNode("ANIMATION"));
            Close();
        }

    }
}